<?php


namespace taglib;

use think\template\TagLib;

/**
 * XC标签库解析类
 * @category   Think
 * @package  Think
 * @subpackage  Driver.Taglib
 * @author    liu21st <liu21st@gmail.com>
 */
class Sp extends Taglib
{

    // 标签定义
    protected $tags = [
        'artlist' => ['attr' => 'cate_id,id,start,limit,order', 'close' => 0],

        'articles' => ['attr'=> 'field,id,limit,cid,order,is_home,returnname', 'close'=>1]

    ];

    /**
     * artlist 文章列表
     * 格式：
     * {sp:artlist cid="1" field='id,title' limit="2" order="id asc" id="vo" is_home="1"}
     * {data.username}
     * {data.email}
     * {/sp:artlist}
     * @param $tag
     * @param $content
     * @return string
     */
    public function tagArtlist($tag, $content)
    {
        //$cate_id = $this->autoBuildVar($tag['cate_id']);
        $cate_id= !empty($tag['cate_id']) ? $tag['cate_id'] : 0;
        $id     = $tag['id'];
        $start= !empty($tag['start']) ? $tag['start'] : 0;
        $limit = !empty($tag['limit']) ? $tag['limit'] : 100;
        $order = !empty($tag['order']) ? $tag['order'] : "sort asc,a_id desc";
        if($cate_id){

        }else{
            $parseStr = '<?php $data=""; ?>';
            $parseStr .= $content;
        }

        //return $parseStr;
    }


    public function tagArticles($tag, $content)
    {

        $id = $tag['id'] ? $tag['id'] : 'vo';
        $order = empty($tag['order']) ? "'sort asc,a_id DESC'" : '"' . $tag['order'] . '"';
        $is_home = empty($tag['is_home']) ? "0" : $tag['is_home'];
        $returnname = 'article_data';
        $field = "''";
        if (!empty($tag['field'])) {
            if (strpos($tag['field'], '$') === 0) {
                $field = $tag['field'];
                $this->autoBuildVar($field);
            } else {
                $field = "'{$tag['field']}'";
            }
        }
        $cid = "''";
        if (!empty($tag['cid'])) {
            if (strpos($tag['cid'], '$') === 0) {
                $cid = $tag['cid'];
                $this->autoBuildVar($cid);
            } else {
                $cid = "'{$tag['cid']}'";
            }
        }

        $limit = "''";
        if (!empty($tag['limit'])) {
            if (strpos($tag['limit'], '$') === 0) {
                $limit = $tag['limit'];
                $this->autoBuildVar($limit);
            } else {
                $limit = "'{$tag['limit']}'";
            }
        } else {
            $limit = '0,5';
        }
        $parse = <<<parse
<?php
    \$$returnname = \app\common\service\ArticleService::taglib_article_list([
    'field'   => {$field},
    'cid'=>{$cid},
    'limit'=>{$limit},
    'is_home'=>{$is_home},
    'order'=>{$order}
]);
?>
{volist name="{$returnname}" id="{$id}"}
{$content}
{/volist}
parse;
        return $parse;
    }


}
